// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.randomizedWorld.randomizedBuilding;

import zombie.core.Rand;
import zombie.inventory.ItemPickerJava;
import zombie.iso.BuildingDef;
import zombie.iso.IsoCell;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.IsoWorld;
import zombie.iso.SpawnPoints;
import zombie.iso.objects.IsoBarricade;
import zombie.iso.objects.IsoDoor;
import zombie.iso.objects.IsoWindow;
import zombie.network.GameServer;

public final class RBSafehouse extends RandomizedBuildingBase {
    @Override
    public void randomizeBuilding(BuildingDef def) {
        def.bAlarmed = false;
        def.setHasBeenVisited(true);
        ItemPickerJava.ItemPickerRoom itemPickerRoom = ItemPickerJava.rooms.get("SafehouseLoot");
        IsoCell isoCell = IsoWorld.instance.CurrentCell;

        for (int int0 = def.x - 1; int0 < def.x2 + 1; int0++) {
            for (int int1 = def.y - 1; int1 < def.y2 + 1; int1++) {
                for (int int2 = 0; int2 < 8; int2++) {
                    IsoGridSquare isoGridSquare0 = isoCell.getGridSquare(int0, int1, int2);
                    if (isoGridSquare0 != null) {
                        for (int int3 = 0; int3 < isoGridSquare0.getObjects().size(); int3++) {
                            IsoObject isoObject = isoGridSquare0.getObjects().get(int3);
                            if (isoObject instanceof IsoDoor && ((IsoDoor)isoObject).isBarricadeAllowed() && !SpawnPoints.instance.isSpawnBuilding(def)) {
                                IsoGridSquare isoGridSquare1 = isoGridSquare0.getRoom() == null ? isoGridSquare0 : ((IsoDoor)isoObject).getOppositeSquare();
                                if (isoGridSquare1 != null && isoGridSquare1.getRoom() == null) {
                                    boolean boolean0 = isoGridSquare1 != isoGridSquare0;
                                    IsoBarricade isoBarricade0 = IsoBarricade.AddBarricadeToObject((IsoDoor)isoObject, boolean0);
                                    if (isoBarricade0 != null) {
                                        int int4 = Rand.Next(1, 4);

                                        for (int int5 = 0; int5 < int4; int5++) {
                                            isoBarricade0.addPlank(null, null);
                                        }

                                        if (GameServer.bServer) {
                                            isoBarricade0.transmitCompleteItemToClients();
                                        }
                                    }
                                }
                            }

                            if (isoObject instanceof IsoWindow) {
                                IsoGridSquare isoGridSquare2 = isoGridSquare0.getRoom() == null ? isoGridSquare0 : ((IsoWindow)isoObject).getOppositeSquare();
                                if (((IsoWindow)isoObject).isBarricadeAllowed() && int2 == 0 && isoGridSquare2 != null && isoGridSquare2.getRoom() == null) {
                                    boolean boolean1 = isoGridSquare2 != isoGridSquare0;
                                    IsoBarricade isoBarricade1 = IsoBarricade.AddBarricadeToObject((IsoWindow)isoObject, boolean1);
                                    if (isoBarricade1 != null) {
                                        int int6 = Rand.Next(1, 4);

                                        for (int int7 = 0; int7 < int6; int7++) {
                                            isoBarricade1.addPlank(null, null);
                                        }

                                        if (GameServer.bServer) {
                                            isoBarricade1.transmitCompleteItemToClients();
                                        }
                                    }
                                } else {
                                    ((IsoWindow)isoObject).addSheet(null);
                                    ((IsoWindow)isoObject).HasCurtains().ToggleDoor(null);
                                }
                            }

                            if (isoObject.getContainer() != null
                                && isoGridSquare0.getRoom() != null
                                && isoGridSquare0.getRoom().getBuilding().getDef() == def
                                && Rand.Next(100) <= 70
                                && isoGridSquare0.getRoom().getName() != null
                                && itemPickerRoom.Containers.containsKey(isoObject.getContainer().getType())) {
                                isoObject.getContainer().clear();
                                ItemPickerJava.fillContainerType(itemPickerRoom, isoObject.getContainer(), "", null);
                                ItemPickerJava.updateOverlaySprite(isoObject);
                                isoObject.getContainer().setExplored(true);
                            }
                        }
                    }
                }
            }
        }

        def.setAllExplored(true);
        def.bAlarmed = false;
        this.addZombies(def);
    }

    private void addZombies(BuildingDef buildingDef) {
        this.addZombies(buildingDef, 0, null, null, null);
        if (Rand.Next(5) == 0) {
            this.addZombies(buildingDef, 1, "Survivalist", null, null);
        }

        if (Rand.Next(100) <= 60) {
            RandomizedBuildingBase.createRandomDeadBody(this.getLivingRoomOrKitchen(buildingDef), Rand.Next(3, 7));
        }

        if (Rand.Next(100) <= 60) {
            RandomizedBuildingBase.createRandomDeadBody(this.getLivingRoomOrKitchen(buildingDef), Rand.Next(3, 7));
        }
    }

    @Override
    public boolean isValid(BuildingDef buildingDef, boolean _boolean) {
        if (!super.isValid(buildingDef, _boolean)) {
            return false;
        } else {
            return buildingDef.getRooms().size() > 20 ? false : !SpawnPoints.instance.isSpawnBuilding(buildingDef);
        }
    }

    public RBSafehouse() {
        this.name = "Safehouse";
        this.setChance(10);
    }
}
